package com.gitee.wsl.common.chart.render.shape.bar

import android.graphics.*
import com.gitee.wsl.android.ext.base.setAlphaComponent
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.ui.draw.type.GridDirection
import com.gitee.wsl.ext.base.ColorExt

import timber.log.Timber
import kotlin.math.abs

class FlatBar: Bar() {
    
    //柱形填充色透明度
    var fillAlpha = 255

    var linearGradient: LinearGradient? = null
    var path: Path = Path()

    var radius = 5 //角半径


    /**
     * 计算同标签多柱形时的Y分隔
     * @param YSteps    Y轴步长
     * @param barNumber  柱形个数
     * @return 返回单个柱形的高度及间距
     */
    fun getBarHeightAndMargin(YSteps: Float, barNumber: Int): FloatArray? {
        return calcBarHeightAndMargin(YSteps, barNumber)
    }

    /**
     * 计算同标签多柱形时的X分隔
     * @param XSteps    X轴步长
     * @param barNumber 柱形个数
     * @return 返回单个柱形的宽度及间距
     */
    fun getBarWidthAndMargin(XSteps: Float, barNumber: Int): FloatArray? {
        return calcBarWidthAndMargin(XSteps, barNumber)
    }

    /**
     * 绘制柱形渲染效果
     * @param left    左边X坐标
     * @param top    顶部Y坐标
     * @param right    右边X坐标
     * @param bottom    底部Y坐标
     */
    private fun setBarGradient(left: Float, top: Float, right: Float, bottom: Float) {
        val barColor: Int = barPaint.color
        val lightColor = ColorExt.setAlphaComponent(barColor, 150)
        val width = abs(right - left)
        val height = abs(bottom - top)
        val tm = Shader.TileMode.MIRROR
        linearGradient = if (width > height) //横向柱形
        {
            LinearGradient(
                right, bottom, right, top, intArrayOf(lightColor, barColor),
                null, tm
            )
        } else {
            LinearGradient(
                left, bottom, right, bottom, intArrayOf(lightColor, barColor),
                null, tm
            )
        }
        barPaint.shader = linearGradient
    }


    /**
     * 绘制柱形
     * @param left    左边X坐标
     * @param top    顶部Y坐标
     * @param right    右边X坐标
     * @param bottom    底部Y坐标
     * @param canvas    画布
     */
    fun renderBar(left: Float, top: Float, right: Float, bottom: Float, canvas: Canvas): Boolean {
        val style: ChartEnum.BarStyle = barStyle
        if (top.compareTo(bottom) == 0) return true
        if (ChartEnum.BarStyle.ROUNDBAR == style) {
            canvas.drawRoundRect(
                RectF(left, bottom, right, top),
                barRoundRaidus, barRoundRaidus, barPaint
            )
            return true
        }

        if (ChartEnum.BarStyle.OUTLINE == style) {
            val barColor: Int = barPaint.color
            val lightColor = ColorExt.setAlphaComponent(barColor, outlineAlpha)
            outlineBarPaint.color = lightColor
            canvas.drawRect(left, bottom, right, top, outlineBarPaint)
            barPaint.style = Paint.Style.STROKE
            //mPaintBar.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));		
            barPaint.strokeWidth = borderWidth.toFloat()
            drawPathBar(left, top, right, bottom, canvas)
            barPaint.strokeWidth = borderWidth.toFloat() //pWidth);
            return true
        } else if (ChartEnum.BarStyle.TRIANGLE == style) {
            var mid = 0.0f
            when (this.barGridDirection) {
                GridDirection.HORIZONTAL -> {
                    mid = top + (bottom - top) / 2
                    path.moveTo(left, top)
                    path.lineTo(right, mid)
                    path.lineTo(left, bottom)
                    path.close()
                    canvas.drawPath(path, barPaint)
                    canvas.drawCircle(right, mid, radius.toFloat(), barPaint)
                }
                else -> {
                    mid = left + (right - left) / 2
                    path.moveTo(left, bottom)
                    path.lineTo(mid, top)
                    path.lineTo(right, bottom)
                    path.close()
                    canvas.drawPath(path, barPaint)
                    canvas.drawCircle(mid, top, radius.toFloat(), barPaint)
                }
            }
            path.reset()
            return true
        } else {
            //GRADIENT,FILL,STROKE
            when (style) {
                ChartEnum.BarStyle.GRADIENT -> setBarGradient(left, top, right, bottom)
                ChartEnum.BarStyle.FILL -> barPaint.style = Paint.Style.FILL
                ChartEnum.BarStyle.STROKE -> {
                    if (1f.compareTo(barPaint.strokeWidth) == 0)
                        barPaint.strokeWidth = 3f
                    barPaint.style = Paint.Style.STROKE
                }
                ChartEnum.BarStyle.TRIANGLE, ChartEnum.BarStyle.OUTLINE -> {}
                else -> {
                    Timber.d( "不认识的柱形风格参数.")
                    return false
                }
            }
            if (barStyle != ChartEnum.BarStyle.FILL) {
                setBarGradient(left, top, right, bottom)
            }
            drawPathBar(left, top, right, bottom, canvas)
        }
        return true
    }

    /**
     * 绘制柱形标签
     * @param text    文本内容
     * @param x    x坐标
     * @param y    y坐标
     * @param canvas    画布
     */
    fun renderBarItemLabel(text: String, x: Float, y: Float, canvas: Canvas) {
        drawBarItemLabel(text, x, y, canvas)
    }

    private fun drawPathBar(left: Float, top: Float, right: Float, bottom: Float, canvas: Canvas) {
        path.moveTo(left, bottom)
        path.lineTo(left, top)
        path.lineTo(right, top)
        path.lineTo(right, bottom)
        path.close()
        canvas.drawPath(path, barPaint)
        path.reset()
    }
    
}